package com.github.itdachen.quartz.controller;

import com.github.itdachen.framework.core.response.ServerResponse;
import com.github.itdachen.quartz.entity.CronJobInfo;
import com.github.itdachen.quartz.entity.CronJobResp;
import com.github.itdachen.quartz.service.IQuartzJobService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务调用
 *
 * @author 剑鸣秋朔
 * @date 2025/11/21 21:42
 */
@RestController
@RequestMapping("/job")
public class QuartzJobController {

    private final IQuartzJobService quartzJobService;

    public QuartzJobController(IQuartzJobService quartzJobService) {
        this.quartzJobService = quartzJobService;
    }

    /***
     * 查询
     *
     * @author 剑鸣秋朔
     * @date 2025/11/21 22:12
     * @return com.github.itdachen.framework.core.response.ServerResponse<java.util.List<com.github.itdachen.topic.quartz.entity.CronJobResp>>
     */
    @GetMapping("/query")
    public ServerResponse<List<CronJobResp>> query() throws Exception {
        return ServerResponse.ok(quartzJobService.query());
    }


    /***
     * 手动执行
     *
     * @author 剑鸣秋朔
     * @date 2025/11/21 22:10
     * @param cronJobInfo cronJobInfo
     * @return com.github.itdachen.framework.core.response.ServerResponse<java.lang.Object>
     */
    @RequestMapping(value = "/run")
    public ServerResponse<Object> run(@RequestBody CronJobInfo cronJobInfo) throws Exception {
        quartzJobService.run(cronJobInfo);
        return ServerResponse.ok();
    }


    /***
     * 新增任务
     *
     * @author 剑鸣秋朔
     * @date 2025/11/21 21:48
     * @param cronJobInfo cronJobInfo
     * @return void
     */
    @PostMapping("")
    public ServerResponse<Object> save(@RequestBody CronJobInfo cronJobInfo) throws Exception {
        quartzJobService.save(cronJobInfo);
        return ServerResponse.ok();
    }


    /***
     * 重置、修改
     *
     * @author 剑鸣秋朔
     * @date 2025/11/21 21:50
     * @param cronJobInfo cronJobInfo
     * @return void
     */
    @PostMapping("/reschedule")
    public ServerResponse<Object> reschedule(@RequestBody CronJobInfo cronJobInfo) throws Exception {
        quartzJobService.reschedule(cronJobInfo);
        return ServerResponse.ok();
    }

    /***
     * 暂停任务
     *
     * @author 剑鸣秋朔
     * @date 2025/11/21 21:49
     * @param cronJobInfo cronJobInfo
     * @return void
     */
    @PostMapping("/pause")
    public ServerResponse<Object> pause(@RequestBody CronJobInfo cronJobInfo) throws Exception {
        quartzJobService.pause(cronJobInfo);
        return ServerResponse.ok();
    }

    /***
     * 重启任务
     *
     * @author 剑鸣秋朔
     * @date 2025/11/21 21:50
     * @param cronJobInfo cronJobInfo
     * @return void
     */
    @PostMapping("/resume")
    public ServerResponse<Object> resume(@RequestBody CronJobInfo cronJobInfo) throws Exception {
        quartzJobService.resume(cronJobInfo);
        return ServerResponse.ok();
    }


    /***
     * 删除任务
     *
     * @author 剑鸣秋朔
     * @date 2025/11/21 21:51
     * @param cronJobInfo cronJobInfo
     * @return void
     */
    @PostMapping("/delete")
    public ServerResponse<Object> delete(@RequestBody CronJobInfo cronJobInfo) throws Exception {
        quartzJobService.delete(cronJobInfo);
        return ServerResponse.ok();
    }

}
